package com.hhxy.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ghp
 * @date 2023/2/25
 * @title 队列、交换机配置类（基于死信队列实现延迟队列）
 * @description
 */
@Configuration
public class TtlQueueConfig {

    // 用于声明死信交换机(特定写法)
    public static final String DEAD_LETTER_EXCHANGE = "x-dead-letter-exchange";
    // 用于声明死信交换机的路由键(特定写法)
    public static final String X_DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

    public static final String X_MESSAGE_TTL = "x-message-ttl";

    // 普通交换机
    public static final String X_EXCHANGE = "X";
    // 死信交换机
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";
    // 普通队列
    public static final String A_QUEUE = "A";
    public static final String B_QUEUE = "B";
    public static final String T_QUEUE = "T";
    // 死信队列
    public static final String C_DEAD_LETTER_QUEUE = "C";

    /**
     * 声明普通交换机 X
     *
     * @return
     */
    @Bean("xExchange")
    public DirectExchange xExchange() {
        return new DirectExchange(X_EXCHANGE);
    }

    /**
     * 声明死信交换机 Y
     */
    @Bean("yExchange")
    public DirectExchange yExchange() {
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    /**
     * 声明普通队列 A
     */
    @Bean("A")
    public Queue queueA() {
        // 配置队列的参数
        Map<String, Object> arguments = new HashMap<>();
        // 配置死信交换机
        arguments.put(DEAD_LETTER_EXCHANGE, Y_DEAD_LETTER_EXCHANGE);
        // 为死信交换机配置路由键
        arguments.put(X_DEAD_LETTER_ROUTING_KEY, "CY");
        // 为 A 队列设置TTL 10s
        arguments.put(X_MESSAGE_TTL, 10000);

        return QueueBuilder.durable(A_QUEUE).withArguments(arguments).build();
    }

    /**
     * 声明普通队列 B
     */
    @Bean("B")
    public Queue queueB() {
        // 配置队列的参数
        Map<String, Object> arguments = new HashMap<>();
        // 配置死信交换机
        arguments.put(DEAD_LETTER_EXCHANGE, Y_DEAD_LETTER_EXCHANGE);
        // 为死信交换机配置路由键
        arguments.put(X_DEAD_LETTER_ROUTING_KEY, "CY");
        // 为 B 队列设置TTL 60s
        arguments.put(X_MESSAGE_TTL, 60000);

        return QueueBuilder.durable(B_QUEUE).withArguments(arguments).build();
    }

    /**
     * 声明一个普通队列 T（不直接指定TTL）
     */
    @Bean("T")
    public Queue queueT(){
        // 配置队列的参数
        Map<String, Object> arguments = new HashMap<>();
        // 配置死信交换机
        arguments.put(DEAD_LETTER_EXCHANGE, Y_DEAD_LETTER_EXCHANGE);
        // 为死信交换机配置路由键
        arguments.put(X_DEAD_LETTER_ROUTING_KEY, "CY");
        return QueueBuilder.durable(T_QUEUE).withArguments(arguments).build();
    }

    /**
     * 声明死信队列 C
     */
    @Bean("C")
    public Queue queueC() {
        return QueueBuilder.durable(C_DEAD_LETTER_QUEUE).build();
    }

    /**
     * 队列 A 绑定交换机 X
     */
    @Bean
    public Binding aQueueBindingX(@Qualifier("A") Queue aQueue, @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(aQueue).to(xExchange).with("AX");
    }

    /**
     * 队列 B 绑定交换机 X
     */
    @Bean
    public Binding bQueueBindingX(@Qualifier("B") Queue aQueue, @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(aQueue).to(xExchange).with("BX");
    }

    /**
     * 队列 T 绑定交换机 X
     */
    @Bean
    public Binding tQueueBindingX(@Qualifier("T") Queue tQueue, @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(tQueue).to(xExchange).with("TX");
    }

    /**
     * 队列 A 绑定交换机 Y
     */
    @Bean
    public Binding aQueueBindingY(@Qualifier("A") Queue aQueue, @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(aQueue).to(yExchange).with("AY");
    }

    /**
     * 队列 B 绑定交换机 Y
     */
    @Bean
    public Binding bQueueBindingY(@Qualifier("B") Queue bQueue, @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(bQueue).to(yExchange).with("BY");
    }

    /**
     * 队列 T 绑定交换机 Y
     */
    @Bean
    public Binding tQueueBindingY(@Qualifier("T") Queue tQueue, @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(tQueue).to(yExchange).with("TY");
    }

    /**
     * 队列 C 绑定交换机 Y
     */
    @Bean
    public Binding cQueueBindingY(@Qualifier("C") Queue cQueue, @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(cQueue).to(yExchange).with("CY");
    }


}
